local jiaoting = fk.CreateSkill{
  name = "rfenghou_3d18j__jiaoting",
  max_round_use_time = 1,
  dynamic_desc = function(self, player, lang)
    if player:getMark("@@rfenghou_3d18j__youxing-round") ~= 0 then
      return Fk:translate(":rfenghou_3d18j__jiaoting-youxing")
    end
  end
}

jiaoting:addEffect(fk.CardUseFinished, {
  can_trigger = function(self, event, target, player, data)
    if not (player:hasSkill(jiaoting.name) and data.card) or player:isNude() then return end
    local card = data.card
    local markname = {"slash", "peach"}
    if player:getMark("@@rfenghou_3d18j__youxing-round") ~= 0 then
      table.insert(markname, "jink")
      table.insert(markname, "analeptic")
    end
    if not table.contains(markname, card.trueName) then return end
    return data.from == player or table.contains(data:getAllTargets(), player)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local card_name = data.card.trueName
    local markname = {"slash", "peach"}
    if player:getMark("@@rfenghou_3d18j__youxing-round") ~= 0 then
      table.insert(markname, "jink")
      table.insert(markname, "analeptic")
    end
    table.removeOne(markname, card_name)
    local _, ret = room:askToUseActiveSkill(player, {
      skill_name = "#rfenghou_3d18j__jiaoting_active",
      prompt = "#rfenghou_3d18j__jiaoting-invoke0:::" .. card_name,
      extra_data = {
        skillName = jiaoting.name,
        card_name = card_name,
        another_name = markname,
        bypass_times = true,
      },
      cancelable = true,
      skip = true,
    })

    if ret then
      event:setCostData(self, { cards = ret.cards, tos = ret.targets, card_name = card_name, another_name = markname })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dat = event:getCostData(self)
    local card_name, cards = dat.card_name, dat.cards
    local card = Fk:cloneCard(card_name)
    card.skillName = jiaoting.name
    room:useCard{
      card = card,
      from = player,
      tos = dat.tos,
    }
    room:throwCard(cards, jiaoting.name, player, player)
    if #cards == 2 then
      room:askToUseVirtualCard(player, {
        name = dat.another_name,
        skill_name = jiaoting.name,
        cancelable = false,
        extra_data = {
          skillName = jiaoting.name,
          bypass_times = true,
        }
      })
    elseif #cards == 1 then
      room:addPlayerMark(player, "@rfenghou_3d18j__jiaoting-turn")
    end

    local ex = room:askToUseVirtualCard(player, {
      name = "ex_nihilo",
      skill_name = jiaoting.name,
      prompt = "#rfenghou_3d18j__jiaoting-lose",
      cancelable = true,
      skip = true,
    })
    if ex then
      room:handleAddLoseSkills(player, "-" .. jiaoting.name)
      if player.dead then return end
      room:useCard(ex)
    end
  end
}, { check_skill_limit = true })
jiaoting:addEffect(fk.TurnEnd, {
  is_delay_effect = true,
  anim_type = "negative",
  can_trigger = function(self, event, target, player, data)
    return player:getMark("@rfenghou_3d18j__jiaoting-turn") > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:loseHp(player, 1, jiaoting.name)
    room:setPlayerMark(player, "@rfenghou_3d18j__jiaoting-turn", 0)
  end,
})

Fk:loadTranslationTable{
  ["rfenghou_3d18j__jiaoting"] = "浇廷",
  [":rfenghou_3d18j__jiaoting"] = "每轮限一次，一张【杀】或【桃】使用结算后，若你是此牌的使用者或目标，可视为使用之并弃置一至两张牌，若因此弃置的牌数为：一，此回合结束时，你失去1点体力；二，你视为使用另一张牌。然后，你可失去此技能，视为使用一张【无中生有】。",
  [":rfenghou_3d18j__jiaoting-youxing"] = "每轮限一次，一张【杀】、【闪】、【酒】或【桃】使用结算后，若你是此牌的使用者或目标，可视为使用之并弃置一至两张牌，若因此弃置的牌数为：一，此回合结束时，你失去1点体力；二，你视为使用另一张牌。然后，你可失去此技能，视为使用一张【无中生有】。",

  ["#rfenghou_3d18j__jiaoting-invoke0"] = "浇廷：你可以弃置一至两张牌，视为使用【%arg】",
  ["#rfenghou_3d18j__jiaoting-lose"] = "浇廷：你可以失去此技能，视为使用一张【无中生有】",

  ["@@rfenghou_3d18j__jiaoting-turn"] = "浇廷",
}

return jiaoting
